<?php
/**
 *
 *
 * This file is part of Webappkit, a packages system for PHP web apps
 * @link http://webappkit.net
 * @package
 * @subpackage
 * @author J.Ducastel <nospam0@ducastel.name>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

/**
 * <select>
 * @property array $choices
 */
class DjSelect extends DjWidget {

    public function render($name, $value=null, $attributes=array()) {
        $html = '<select name="'.$name.'" '
        .$this->_renderAttributes($attributes).'>';
        foreach ($this->choices as $key => $choice) {
            $html .= $this->_option($choice, $key, $value==$choice);
        }
        return $html.'</select>';
    }

    /**
     * return <option> tag
     * @param mixed $value
     * @param mixed $label if string, will be used as option label
     * @param bool $selected
     * @return string
     */
    protected function _option($value, $label, $selected = false) {
        return '<option value="'.$value.'">'
        .(is_int($label)?$value:$label)
        .'</option>';
    }

    protected $choices = array();
}